package com.cjm.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

public class WordPattern290 {
    public static boolean wordPattern(String pattern, String s) {
        String[] s1 = s.split(" ");
        char[] chars = pattern.toCharArray();

        if (s1.length != chars.length) {
            throw new IllegalArgumentException();
        }

        Map<String, String> map = new HashMap<>();
        Map<String, String> map1 = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            String patternStr = String.valueOf(chars[i]);
            String wordStr = s1[i];

            // -------------第一种情况
            String s2 = map.get(patternStr);
            if (s2 != null && !s2.equals(wordStr)) {
                return false;
            }

            // ---------------第二种情况
            String s3 = map1.get(wordStr);
            if (s3!=null && !s3.equals(patternStr)) {
                return false;
            }


            map1.put(wordStr, patternStr);
            map.put(patternStr, wordStr);
        }

        return true;
    }

    public static void main(String[] args) {
        System.err.println(wordPattern("abba", "dog cat cat dog"));
        System.err.println(wordPattern("abba", "dog cat cat fish"));
        System.err.println(wordPattern("aaaa", "dog cat cat dog"));
    }
}
